package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.work.WorkEntryBO;
import com.ymk.core.code.cmd.WorkEntryCmdCode;
import com.ymk.service.IWorkEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Service
public class WorkEntryOrNotCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkEntryBO reqData = super.getData(sign, WorkEntryBO.class);
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("isEntry",true)
                .addParam("userId",reqData.getUserId())
                .addParam("workId",reqData.getWorkId());

        WorkEntryBO workEntryBO = workEntryService.get(paramsBuilder.build());
        if (Objects.nonNull(workEntryBO)){
            throw new MallException( "已申请入职，详情可前往我的页面查看入职记录");
        }

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return WorkEntryCmdCode.WORK_ENTRY_OR_NOT;
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Autowired
    private IWorkEntryService workEntryService;
}